package com.example.myapplication.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

/**
 * 缩放
 * 作者：李祥
 * 邮箱：499194689@qq.com
 */

public class TestView02 extends View {
    Paint paint=new Paint();
    int mHeight;
    int mWidth;
    public TestView02(Context context) {
        super(context);
    }

    public TestView02(Context context,  AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mHeight=h;
        mWidth=w;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //将坐标系原点移动到画布正中心
        canvas.translate(mWidth/2,mHeight/2);

        RectF rectF=new RectF(0,-100,100,0); //矩形区域
        paint.setColor(Color.BLACK);                                  //绘制黑色矩形
        paint.setStyle(Paint.Style.STROKE);
        canvas.drawRect(rectF,paint);

        canvas.scale(0.5f,0.5f);                              //画布缩放
        paint.setColor(Color.BLUE);                                   //绘制蓝色矩形
        canvas.drawRect(rectF,paint);

        canvas.scale(0.3f,0.3f,50,0);                //画布缩放并向右移动50个单位
        paint.setColor(Color.RED);
        canvas.drawRect(rectF,paint);

        canvas.scale(-5.5f,-5.5f,-50,0);
        paint.setColor(Color.BLUE);
        canvas.drawRect(rectF,paint);
    }
}
